var ctx;
var title = "";
var type = -1;
var state = -1;
var level = -1;
var pSize;

var beginDate = "";
var endDate = "";

function setCtx(ctx){
	this.ctx = ctx;
}

function checkDate () {
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
    var start=new Date(beginDate.replace("-", "/").replace("-", "/"));  
    var end = new Date(endDate.replace("-", "/").replace("-", "/"));  
    
    if(end < start){
    	layer.msg("结束时间应大于起始时间",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
    	return false;
    }
    return true;
}

//判断是否为空
function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//条件搜索用户列表
function getRepairInfoList(){
	title = ifNull($("#title").val());
	type = $("#infoType").val();
	state = $("#state").val();
	level = $("#level").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(title == $("#title").attr("placeholder")){
		title = "";
	}
	if (checkDate()) {
		loadContent("/repairInfo/rInfoList?pNo=1&title="+ title + "&state="+ state+ "&type="+ type+ "&level="+ level
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
	
}

//跳转到指定页面
function gotoRepairInfoPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	title = ifNull($("#title").val());
	type = $("#infoType").val();
	state = $("#state").val();
	level = $("#level").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(title == $("#title").attr("placeholder")){
		title = "";
	}
	if (checkDate()) {
		loadContent("/repairInfo/rInfoList?pNo="+ page +"&title="+ title+ "&state="+ state+ "&type="+ type+ "&level="
				+ level+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
}

//跳转到上一页或者下一页
function gotoPage(page){
	title = ifNull($("#title").val());
	type = $("#infoType").val();
	state = $("#state").val();
	level = $("#level").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	if (checkDate()) {
		loadContent("/repairInfo/rInfoList?pNo="+ page +"&title="+ title + "&state="+ state+ "&type="+ type+ "&level="+ level
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
}

//新增或更新报修常见问题(1:新增 2:编辑)
function saveOrUpdate(id) {
	if (id != null) {
		loadContent("/repairInfo/editrepairInfo?id=" + id + "");
	} else {
		loadContent("/repairInfo/editrepairInfo?id=''");
	}

}

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    

}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#repairInfo" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#repairInfo" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

//删除多个id对应的记录
function deleteByIds(){
	if($("img[name='repairInfoChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='repairInfoChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/repairInfo/deleteInfo",{
					iDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/repairInfo/rInfoList");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}

//保存备注信息
var index;
function saveOrUpdateMemo(msg,url,id){
  //官网欢迎页
   index = layer.open({
      type: 2,
      //skin: 'layui-layer-lan',
      title: msg,
      fix: false,
      shadeClose: true,
      scrollbar: false,
      maxmin: false,
      edit: true,
      closeBtn: 1,
      area: ['500px', '230px'],
      content: url,
      btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.saveMemo(id);

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	    });
}
//保存回复
function saveOrUpdateReply(msg,url,id){
	  //官网欢迎页
	   index = layer.open({
	      type: 2,
	      //skin: 'layui-layer-lan',
	      title: msg,
	      fix: false,
	      shadeClose: true,
	      scrollbar: false,
	      maxmin: false,
	      edit: true,
	      closeBtn: 1,
	      area: ['500px', '243px'],
	      content: url,
	      btn: ['确定', '取消']
		    ,yes: function(index, layero){ //或者使用btn1
		        //按钮【按钮一】的回调
		    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
		    	iframeWin.saveReply(id);

		    },cancel: function(index){ //或者使用btn2
		        //按钮【按钮二】的回调
		    }
		    });
	}

////查看评价的弹出框
//function seeReview(msg,url,id){
//	  //官网欢迎页
//	   index = layer.open({
//	      type: 2,
//	      //skin: 'layui-layer-lan',
//	      title: msg,
//	      fix: false,
//	      shadeClose: true,
//	      scrollbar: false,
//	      maxmin: false,
//	      edit: true,
//	      closeBtn: 1,
//	      area: ['500px', '350px'],
//	      content: url,
//	      btn: ['返回']
//		    ,cancel: function(index){ //或者使用btn2
//		        //按钮【按钮二】的回调
//		    }
//	    });
//	}


//实现字段较长时的隐藏于显示
function mouseOverInfo(item , answer){
	layer.tips(answer, '#answer' + item, {
		  tips: [3, '#2F2F2F'],
		  time: 5000
		});
}

//处理保修单状态
function changeState(id,state){
	$.ajax({
		type : "POST",
		url : ctx + "/repairInfo/changeState",
		cache : false,
		dataType : 'json',
		data: {
			id : id,
			state: state
		},
		success : function(result) {
			if (result.success) {
				layer.msg("操作成功",{
					icon: 1,
					time: 1000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/repairInfo/rInfoList");
				});
			} else {
				layer.msg("操作失败",{
					icon: 2
				});
			}
		}
	});
}

//新增备注
function addMemo(id) {
	
	msg = "添加备注";
	url = ctx + "/repairInfo/editRepairInfo?id="+id+"&type=1";
	saveOrUpdateMemo(msg, url,id);
}

//新增回复
function addReply(id) {
	
	msg = "新增回复";
	url = ctx + "/repairInfo/editRepairInfo?id=0&type=2";
	saveOrUpdateReply(msg, url,id);
}

//查看回复与评价
function Lookreview (id) {
	loadContent("/repairInfo/editRepairInfo?id="+id+"&type=3");
}

//导出报修信息
function exportExcel() {
	title = ifNull($("#title").val());
	type = $("#infoType").val();
	state = $("#state").val();
	level = $("#level").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	total = ifNull($("#total").val());
	
	//解决ie9以下placeholder与value重复的问题
	if(title == $("#title").attr("placeholder")){
		title = "";
	}
	
	if(total >0 && checkDate()) {
		location.href=ctx +"/repairInfo/exportExcel?title="+ title + "&state="+ state+ "&type="+ type+ "&level="+ level
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate;
	} else {
		layer.msg("未获取到数据，无法导出...",{
			icon: 2,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}
	
}
